package com.duoduo.common.rokectmq.config;

import org.apache.rocketmq.spring.support.DefaultRocketMQListenerContainer;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.StringUtils;

@Configuration
@EnableConfigurationProperties(RocketEnhanceProperties.class)
public class EnvironmentIsolationConfig implements BeanPostProcessor {
	
	@Autowired
    private RocketEnhanceProperties pocketEnhanceProperties;
  
    /**
     * 在装载Bean之前实现参数修改
     */
    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        if(bean instanceof DefaultRocketMQListenerContainer){

            DefaultRocketMQListenerContainer container = (DefaultRocketMQListenerContainer) bean;
					  //拼接Topic
            if(pocketEnhanceProperties.isEnabledIsolation() && StringUtils.hasText(pocketEnhanceProperties.getEnvironment())){
                container.setTopic(String.join("_", container.getTopic(),pocketEnhanceProperties.getEnvironment()));
            }
            return container;
        }
        return bean;
    }
}
